package org.jboss.seam.wiki.core.preferences.editor;

import java.io.Serializable;
import java.util.List;

import javax.validation.constraints.NotNull;

import org.jboss.seam.wiki.preferences.PreferenceValueTemplate;
import org.jboss.seam.wiki.preferences.metamodel.PreferenceEntity;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component("editorSelectOne")
public class EditorSelectOne implements Serializable, ApplicationContextAware {

    /**
	 * 
	 */
	private static final long serialVersionUID = 5497388933963541772L;
	private ApplicationContext applicationContext;

	public List<String> getAllValues(PreferenceEntity.Property property) {
        if (property.getTemplateComponentName() == null || property.getTemplateComponentName().length() == 0)
            throw new RuntimeException("No value template component name " + property);

        PreferenceValueTemplate template = applicationContext.getBean(property.getTemplateComponentName(), PreferenceValueTemplate.class);
        if (template == null) {
            throw new RuntimeException("Couldn't find template component name: " + property.getTemplateComponentName());
        }
        return template.getTemplateValues();

    }

    public boolean isNullable(PreferenceEntity.Property property) {
        return !property.getField().isAnnotationPresent(NotNull.class);
    }

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
}